package com.woniu.apidriver.service;

import com.woniu.internalcommon.constant.CommonStatusEnum;
import com.woniu.internalcommon.constant.IdentityConstants;
import com.woniu.internalcommon.dto.*;
import com.woniu.internalcommon.constant.DriverCarConstants;
import com.woniu.internalcommon.remote.ServiceDriverUserClient;
import com.woniu.internalcommon.remote.ServiceOrderClient;
import com.woniu.internalcommon.request.DriverGrabRequest;
import com.woniu.internalcommon.request.OrderRequest;

import com.woniu.internalcommon.response.OrderDriverResponse;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class OrderService {
    @Resource
    ServiceOrderClient serviceOrderClient;

    @Resource
    ServiceDriverUserClient serviceDriverUserClient;


    /**
     * 去接乘客上车
     * @param orderRequest
     * @return
     */
    public ResponseResult toPickUpPassenger(OrderRequest orderRequest) {
        return serviceOrderClient.toPickUpPassenger(orderRequest);
    }


    /**
     * 到达乘客上车点
     * @param orderRequest
     * @return
     */
    public ResponseResult arrivedDeparture(OrderRequest orderRequest) {
        return serviceOrderClient.arrivedDeparture(orderRequest);
    }

    /**
     * 接到乘客
     * @param orderRequest
     * @return
     */
    public ResponseResult pickUpPassenger(OrderRequest orderRequest) {
        return serviceOrderClient.pickUpPassenger(orderRequest);
    }

    /**
     * 乘客到达目的地，下车
     * @param orderRequest
     * @return
     */
    public ResponseResult passengerGetoff(OrderRequest orderRequest) {
        return serviceOrderClient.passengerGetoff(orderRequest);
    }

    /**
     * 取消订单
     * @param orderId
     * @return
     */
    public ResponseResult cancel(Long orderId) {
        return serviceOrderClient.cancel(orderId, IdentityConstants.DRIVER_IDENTITY);
    }

    /**
     * 订单详情
     * @param orderId
     * @return
     */
    public ResponseResult<OrderInfo> detail(Long orderId) {
        return serviceOrderClient.detail(orderId);
    }

    /**
     * 查询订单当前状态
     * @param phone
     * @param identity
     * @return
     */
    public ResponseResult<OrderInfo> currentOrder(String phone, String identity) {
        return serviceOrderClient.current(phone, identity);
    }

    /**
     * 司机抢单
     * @param orderId
     * @param driverPhone
     * @param driverLongitude  司机经度
     * @param driverLatitude  司机维度
     * @return
     */
    @GlobalTransactional
    public ResponseResult grab(Long orderId, String driverPhone, String driverLongitude, String driverLatitude) {
        //根据司机的电话，查询司机、车辆绑定关系
        ResponseResult<DriverCarBindingRelationship> driverCarRelationShip = serviceDriverUserClient.getDriverCarRelationShip(driverPhone);
        //判断绑定关系是否存在
        if(driverCarRelationShip.getData() ==null){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getCode(),CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getValue());
        }
        // 根据车辆信息，查询司机信息  应该是乘客查看接单司机的信息
        DriverCarBindingRelationship relationship = driverCarRelationShip.getData();
        Long carId = relationship.getCarId();
        ResponseResult<OrderDriverResponse> availableDriverResponseResult = serviceDriverUserClient.getAvailableDriver(carId);
        //判断司机信息是否存在
        if(availableDriverResponseResult.getData() == null){
            return ResponseResult.fail(CommonStatusEnum.CITY_DRIVER_EMPTY.getCode(),CommonStatusEnum.CITY_DRIVER_EMPTY.getValue());
        }
        // 获取司机具体信息，展示给乘客
        OrderDriverResponse orderDriverResponse = availableDriverResponseResult.getData();
        Long driverId = orderDriverResponse.getDriverId();
        String licenseId = orderDriverResponse.getLicenseId();
        String vehicleNo = orderDriverResponse.getVehicleNo();
        String vehicleType = orderDriverResponse.getVehicleType();

        // 封装抢单对象
        DriverGrabRequest driverGrabRequest = new DriverGrabRequest();
        driverGrabRequest.setOrderId(orderId);
        driverGrabRequest.setDriverId(driverId);
        driverGrabRequest.setCarId(carId);
        driverGrabRequest.setDriverPhone(driverPhone);
        driverGrabRequest.setLicenseId(licenseId);
        driverGrabRequest.setVehicleNo(vehicleNo);
        driverGrabRequest.setVehicleType(vehicleType);
        driverGrabRequest.setReceiveOrderCarLatitude(driverLongitude);
        driverGrabRequest.setReceiveOrderCarLongitude(driverLatitude);
        //执行抢单动作
        ResponseResult responseResult = serviceOrderClient.driverGrab(driverGrabRequest);
        if(responseResult.getCode() != CommonStatusEnum.SUCCESS.getCode()){
            // 抢单失败,修改订单信息失败
            return ResponseResult.fail(CommonStatusEnum.ORDER_UPDATE_ERROR.getCode(),CommonStatusEnum.ORDER_UPDATE_ERROR.getValue());
        }
        //抢单成功，修改司机工作状态
        DriverUserWorkStatus driverUserWorkStatus = new DriverUserWorkStatus();
        driverUserWorkStatus.setDriverId(driverId);
        driverUserWorkStatus.setWorkStatus(DriverCarConstants.DRIVER_WORK_STATUS_SERVING);

        ResponseResult responseResult1 = serviceDriverUserClient.changeWorkStatus(driverUserWorkStatus);
        if (responseResult1.getCode() != CommonStatusEnum.SUCCESS.getCode()){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_STATUS_UPDATE_ERROR.getCode(),CommonStatusEnum.DRIVER_STATUS_UPDATE_ERROR.getValue());
        }
        return ResponseResult.success();

    }
}
